package b1.mobile.blackberry.ui.component;

import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.FontFamily;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;

public class PreferenceToggleField extends Manager {

	static int backgroundColor = 0x00f3f3f3;
	static int hlightBackgroundColor = 0x00226ca9;
	
	static double labelWidthRadio = 0.5;
	static double editWidthRadio = 0.5;
	
	static int hPadding = 10;
	static int vPadding = 7;
	static int vMargin = 0;
	
	int screenWidth = Display.getWidth();
	Font editFont = Font.getDefault();
	Font labelFont = Font.getDefault();
	
	LabelField _label = new LabelField("", LabelField.ELLIPSIS | Field.FIELD_VCENTER);
	ToggleField _edit = new ToggleField(0);
	
	public PreferenceToggleField(long style){		
		super(style | Field.FIELD_VCENTER);
		
		try {
			FontFamily ff = FontFamily.forName("Alpha Sans");
			labelFont = ff.getFont(Font.BOLD, 7, Ui.UNITS_pt);
		} catch (ClassNotFoundException e) {
			
		}
		
		setPadding(vPadding, hPadding, vPadding, hPadding);
		
		_label.setText("Highlight");
		_label.setFont(labelFont);
		
		_edit.setMargin(0, 0, 0, 14);
		_edit.setFont(editFont);
		
		add(_label);
		add(_edit);
	}
	// TODO Auto-generated constructor stub}

	protected void sublayout(int width, int height) {
		setPositionChild(_label, 0, 5);
		layoutChild(_label, (int)Math.ceil(labelWidthRadio * screenWidth), 200);
		setPositionChild(_edit, (int)Math.ceil(labelWidthRadio * screenWidth), 0);
		layoutChild(_edit, (int)Math.ceil(editWidthRadio * screenWidth), 35);
		
		this.setExtent(width, this.getPreferredHeight());

	}
	
	protected void subpaint(Graphics graphics) {
		graphics.setColor(isFocus() ? 0x00FFFFFF : 0x00333333);
		paintChild(graphics, _label);
		
//		graphics.setColor(isFocus() ? 0x00808080 : 0x00333333);
		paintChild(graphics, _edit);
	}
	
	public int getPreferredWidth() {
		return super.getPreferredWidth();
	}
	
	public int getPreferredHeight() {
		return 36;
	}
	
	protected void paint(Graphics graphics) {		
		super.paint(graphics);
	}
	
	protected void paintBackground(Graphics arg0) {
		// draw background color depending on the status 
		int bgColor = backgroundColor;
		if(isFocus()) {
			bgColor = hlightBackgroundColor;
		}
		arg0.setColor(bgColor);
		arg0.fillRect(0, 0, getExtent().width, getExtent().height);
		
		arg0.setColor(0x00CCCCCC);
		arg0.drawLine(0, getExtent().height-1, getExtent().width, getExtent().height-1);
		
		super.paintBackground(arg0);
	}
	
	protected int nextFocus(int direction, boolean alt) {
		int index = this.getFieldWithFocusIndex();
		if(alt) {
			if(direction > 0) {
				// Action to perform if trackwheel is rolled up.
			}
			else {
				// Action to perform if trackwheel is rolled down.
			}
		}
		
		if (index == this.getFieldWithFocusIndex())
			return super.nextFocus(direction, alt);
		else
			return index;
	}
	
	public void setValue(boolean value){
		_edit.setBoolean(value);
	}
	
	public boolean getValue(){
		return _edit.getBoolean();
	}

	public void setLabelText(final String string) {
		if(UiApplication.isEventDispatchThread()){
			_label.setText(string);
		} else {
			UiApplication.getApplication().invokeLater(new Runnable(){
				public void run() {
					_label.setText(string);
				}
			});
		}
		
	}

}
